package flc.ast.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyFileBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;

public class MyFileAdapter extends StkProviderMultiAdapter<MyFileBean> {

    public MyFileAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(122));
        addItemProvider(new MyFileProvider());
    }

    private int kind = 0;//0图片 1视频 2音频 3文档

    public void setKind(int kind) {
        this.kind = kind;
    }

    private class MyFileProvider extends BaseItemProvider<MyFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyFileBean bean) {
            baseViewHolder.setText(R.id.tvMyFileItemName, bean.getName());
            baseViewHolder.setText(R.id.tvMyFileItemDate, bean.getDate());
            baseViewHolder.setText(R.id.tvMyFileItemSize, bean.getSize());
            switch (bean.getType()) {
                case 0:
                    baseViewHolder.getView(R.id.ivMyFileItemVideo).setVisibility(View.GONE);
                    Glide.with(getContext()).load(bean.getPath())
                            .into((ImageView) baseViewHolder.getView(R.id.ivMyFileItemImg));
                    break;
                case 1:
                    baseViewHolder.getView(R.id.ivMyFileItemVideo).setVisibility(View.VISIBLE);
                    Glide.with(getContext()).load(bean.getPath())
                            .into((ImageView) baseViewHolder.getView(R.id.ivMyFileItemImg));
                    break;
                case 2:
                    baseViewHolder.setImageResource(R.id.ivMyFileItemImg, R.drawable.wj_yp);
                    break;
                case 3:
                    baseViewHolder.setImageResource(R.id.ivMyFileItemImg, getImgIcon(bean.getPath()));
                    break;
            }
        }
    }

    //根据后缀获取图标
    private int getImgIcon(String path) {
        String suffix = path.substring(path.lastIndexOf("."));
        int icon;
        if (suffix.equals(".pdf") || suffix.equals(".PDF")) {
            icon = R.drawable.wj_pdf;
        } else if (suffix.equals(".ppt") || suffix.equals(".pptx")) {
            icon = R.drawable.wj_ppt;
        } else if (suffix.equals(".txt") || suffix.equals(".TXT")) {
            icon = R.drawable.wj_txt;
        } else if (suffix.equals(".doc") || suffix.equals(".docx")) {
            icon = R.drawable.wj_word;
        } else if (suffix.equals(".xls") || suffix.equals(".xlsx")) {
            icon = R.drawable.wj_excel;
        } else {
            icon = R.drawable.wj_word;
        }
        return icon;
    }
}
